var Console = require("Console");
cc.sdkbox = {
    palayData: ["maxScore"], //玩家储存信息【“最高分”】
    placement_id: "2041233489471536_2042305109364374",

    getPlayID(){ //获取玩家ID
        return FBInstant.player.getID(); 
    },

    getName(){ //获取玩家名称
        return FBInstant.player.getName();
    },

    getPhoto(){ // 获取玩家头像
        return FBInstant.player.getPhoto();
    },

    getDataAsync(obj,func){ //获取玩家储存信息
        FBInstant.player
        .getDataAsync(obj)
        .then(function(data) {
            func && func(data);
        });
    },

    setDataAsync(obj,func){ //储存玩家信息{名： 值}
        FBInstant.player
        .setDataAsync(obj)
        .then(function() {
            func && func();
        });
    },

    getID(){ //获取游戏ID
        return FBInstant.context.getID(); 
    },

    getPlatform(){ // 获取运行平台
        return FBInstant.getPlatform()
    },

    getSupportedAPIs(){ //获取Api 列表
        return FBInstant.getSupportedAPIs()
    },

    shareAsync(name, score){ //分享
        shareImg.share(name, score)
    },

    getLeaderboardAsync(){ // 获取排行榜。
        FBInstant
        .getLeaderboardAsync('my_awesome_leaderboard' + FBInstant.context.getID())
        .then(leaderboard => leaderboard.getEntriesAsync(10, 0))
        .then(entries => {
          for (var i = 0; i < entries.length; i++) {
            console.log(
              entries[i].getRank() + '. ' +
              entries[i].getPlayer().getName() + ': ' +
              entries[i].getScore()
            );
          }
        }).catch(error => console.error(error));
    },

    setScoreAsync(score){ //更新排行榜分数；
        FBInstant.getLeaderboardAsync('my_awesome_leaderboard' + FBInstant.context.getID())
        .then(function(leaderboard) {
          return leaderboard.setScoreAsync(score);
        })
        .then(function(entry) {
            console.log(entry.getScore()); // 42
        });
    },

    getPlayerEntryAsync(){  //获取玩家自身排名
        FBInstant.getLeaderboardAsync('my_awesome_leaderboard' + FBInstant.context.getID())
        .then(function(leaderboard) {
          return leaderboard.getPlayerEntryAsync();
        })
        .then(function(entry) {
          console.log(entry.getRank()); 
          console.log(entry.getScore()); 
        });
    },
    updateAsync(){ //发布更新排行榜
        FBInstant.updateAsync({
            action: 'LEADERBOARD',
            name: 'my_awesome_leaderboard' + FBInstant.context.getID()
          })
        .then(() => console.log('Update Posted'))
        .catch(error => console.error(error));
    },

    getInterstitialAdAsync(){ //尝试创建插页式广告的实例
        // console.log(this.placement_id)
        // var self = this;
        // FBInstant.getRewardedVideoAsync(this.placement_id)
        // .then(function(interstitial) {
        //     self.ad = interstitial;
        //     console.log(interstitial)
        //     return self.ad.loadAsync();
        // })
        // .then(function() {
        //     return self.ad.showAsync();
        // })
        var ad = null;
        FBInstant.getRewardedVideoAsync(
        '2055219064754078_2056899317919386'
        ).then(function(rewardedVideo) {
            console.log(11111)
        ad = rewardedVideo;
        return ad.loadAsync();
        }).then(function() {
        // Ad loaded
            console.log(222)
        return ad.showAsync();
        }).then(function() {
            console.log(333)
        // Ad watched
        });
    },
};

module.exports = cc.sdkbox;

